package edu.cuit.avatar.handler;

import edu.cuit.avatar.entity.Book;
import edu.cuit.avatar.factory.InstanceFactory;
import edu.cuit.avatar.factory.SimpleFactory;
import edu.cuit.avatar.service.BookService;
import edu.cuit.avatar.service.impl.BookServiceImplInFile;
import edu.cuit.avatar.service.impl.BookServiceImplInMemory;

import java.util.Scanner;


/**
 * 接收用户输入的指令进行相应的处理-------控制器
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/15 15:39
 */
public class BookHandler {
    private Scanner input = new Scanner(System.in);
    //IoC
    private BookService service = InstanceFactory.create("book-service");

    /**
     * 读取输入
     * @return 控制台输入内容
     */
    public String readInput(){
        return input.nextLine();
    }

    /**
     * 处理用户输入的指令
     * @param command 指令
     */
    public void process(Command command){
        switch (command) {
            //处理添加图书操作
            case INSERT -> insertBook();
            //处理更新图书操作
            case UPDATE -> updateBook();
            //处理删除图书操作
            case DELETE -> removeBook();
            //处理查找图书操作
            case FIND -> findBook();
            //处理加载所有图书操作
            case ALL -> loadAll();
            //退出系统
            case EXIT -> System.exit(1);
        }
    }

    /**
     * 加载所有图书
     */
    public void loadAll() {
        service.findAll().forEach(System.out::println);
    }

    /**
     * 查找图书
     */
    public void findBook() {
        System.out.println("请输入图书id:");
        Integer id = Integer.valueOf(readInput());
        //调用业务方法完成图书的查找
        var book = service.findById(id);
        if(book == null)
            System.out.println("该图书不存在");
        else
            System.out.println(book);
    }

    /**
     * 删除图书
     */
    public void removeBook() {
        System.out.println("请输入图书id:");
        Integer id = Integer.valueOf(readInput());
        //调用业务方法完成删除
        service.removeById(id);
    }

    /**
     * 更新图书
     */
    public void updateBook() {
        System.out.println("请输入图书id:");
        Integer id = Integer.valueOf(readInput());
        //根据id加载修改前的图书信息
        var book = service.findById(id);
        if(book == null)
            System.out.println("该图书不存在");
        else {
            System.out.println(book);
            System.out.println("请输入图书名称:");
            String title = readInput();
            book.setTitle(title);
            System.out.println("请输入图书作者:");
            String author = readInput();
            book.setAuthor(author);
            System.out.println("请输入图书价格:");
            int price = Integer.parseInt(readInput());
            book.setPrice(price);
            System.out.println("请输入图书出版社:");
            String publisher = readInput();
            book.setPublisher(publisher);
            //调用业务方法完成修改
            service.updateById(book);
        }
    }

    /**
     * 添加图书
     */
    public void insertBook() {
        Book book = new Book();
        System.out.println("请输入图书名称:");
        String title = readInput();
        book.setTitle(title);
        System.out.println("请输入图书作者:");
        String author = readInput();
        book.setAuthor(author);
        System.out.println("请输入图书价格:");
        int price = Integer.parseInt(readInput());
        book.setPrice(price);
        System.out.println("请输入图书出版社:");
        String publisher = readInput();
        book.setPublisher(publisher);

        //调用相关业务模型完成图书的保存
        service.save(book);
    }
}
